package day1201parking;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import day1130.JDBCUtil;

public class CarDao {
	//存入数据
	public static void insert(Car c) {
		Connection conn = null;
		PreparedStatement ps = null;
		String sql = "insert into car(number,height,stime,id) values(?,?,?,?)";
        
		try {
			conn = JDBCUtil.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setString(1, c.getNumber());
			ps.setDouble(2, c.getHeight());
			ps.setLong(3, c.getStime());
			ps.setString(4, c.getId());
			ps.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			JDBCUtil.close(conn, ps);
		}
	}
	
	//查询车的数据
	public static ArrayList<Car> select(String number){
		ArrayList<Car> list = new ArrayList<>();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet result = null;
		String sql = "select * from car where number = ?";
		try {
			conn = JDBCUtil.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setString(1, number);
			result = ps.executeQuery();
			while(result.next()) {
				Car c = new Car();
				c.setNumber(result.getString("number"));
				c.setId(result.getString("id"));
				c.setHeight(result.getDouble("height"));
				c.setStime(result.getLong("stime"));
				list.add(c);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			JDBCUtil.close(conn, ps);
			if(result!=null) {
				try {
					result.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				result = null;
			}
		}
		return list;
	}
	
	//删除汽车的信息
	public static void delete(String number) {
		String sql = "delete from car where number = ?";
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			conn = JDBCUtil.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setString(1, number);
			ps.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			JDBCUtil.close(conn, ps);
		}
	}
	
	

}
